package com.cxh.rmsq.ui.activity.self

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.WindowManager
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.VideoView
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.cxh.rmsq.R
import com.cxh.rmsq.ui.adapter.MediaPagerAdapter
import com.cxh.rmsq.utils.MyUtils

class PreViewImgOrVideoActivity : AppCompatActivity() {
    private lateinit var viewPager: ViewPager2
    private lateinit var iv_back: ImageView
    private lateinit var adapter: MediaPagerAdapter

    //    private val mediaList = listOf(
//        MediaItem(MediaItem.MediaType.IMAGE, "https://example.com/image1.jpg"),
//        MediaItem(MediaItem.MediaType.VIDEO, "https://example.com/video1.mp4"),
//        // 添加更多媒体项...
//    )
    private val mediaList = mutableListOf<String>()
    private val videoViews = mutableListOf<VideoView>() // 记录所有活动的 VideoView

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pre_view_img_or_video)
        findViewById<RelativeLayout>(R.id.cl_toolbar).setPadding(
            0,
            BarUtils.getStatusBarHeight(),
            0,
            0
        )
        BarUtils.setStatusBarColor(this, ColorUtils.getColor(R.color.transparent))
        BarUtils.setStatusBarLightMode(this, true)

        // 进入全屏
        val fullscreenFlag = WindowManager.LayoutParams.FLAG_FULLSCREEN
        window.addFlags(fullscreenFlag)
        window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)

        // 退出全屏
        window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)
        window.clearFlags(fullscreenFlag)

        var url = intent.getStringExtra("url").toString()
        var type = intent.getIntExtra("type", 0)
        if (url.contains(",")) {
            mediaList.addAll(MyUtils.toList(url))

        } else {
            mediaList.add(url)

//            var bean=MediaItem()
//            bean.type = 1
//            bean.url = url!!
//            mediaList.add(bean)
        }
        iv_back = findViewById(R.id.iv_back)
        viewPager = findViewById(R.id.view_pager)
        iv_back.setOnClickListener {
            finish()
        }
        adapter = MediaPagerAdapter(mediaList, this, type)
        viewPager.adapter = adapter

        // 预加载相邻页面
//        viewPager.preload(1)
        viewPager.setOffscreenPageLimit(1)

        // 当页面可见时自动播放视频
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
//                val currentPlayer = adapter.get(position)?.player
//                currentPlayer?.playWhenReady = true
            }
        })

    }

    override fun onDestroy() {
        super.onDestroy()
        // 释放所有 VideoView 资源
        videoViews.forEach { it.pause() }
        videoViews.clear()
    }
}
